#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from typing import Union
from persistent.list import PersistentList
from web_server.models.activities.base_activity import BaseActivity
from web_server.models.ui.uicomponent import UiComponent
from web_server.models.ui.uiscreen import UiScreen
from web_server.models.ui.uiwindow import UiWindow
from common.utils import LogUtil

logger = LogUtil.get_logger(__name__)

class ScopeActivity(BaseActivity):
    _name = "scope"
    _title = "作用域"
    _icon = "bi-bounding-box"
    _category = "ui-automation"
    _scoped = True

    def __init__(self, id: int):
        super().__init__()
        self.id = id
        self.scope: Union[UiScreen, UiWindow, UiComponent, None] = None
        self.activities = PersistentList()

    def _generate_title(self) -> str:
        if self.scope is None:
            return self._title
        else:
            if isinstance(self.scope, UiScreen):
                return f"使用屏幕：{self.scope.get_name()}"
            if isinstance(self.scope, UiWindow):
                return f"使用窗口：{self.scope.get_name()}"
            if isinstance(self.scope, UiComponent):
                return f"使用控件：{self.scope.get_name()}"

    def _generate_args_html(self):
        args_html = ""
        if self.scope is None:
            args_html = '''
            <div class="form-group mb-3">
                <label for="scope-select">Work in scope</label>
                <select class="form-select" id="scope-select">
                    <option value="screen">Screen</option>
                    <option value="window">Window</option>
                    <option value="component">Component</option>
                </select>
            </div>
            '''
        else:
            if isinstance(self.scope, UiScreen):
                args_html = f'''
                <div class="target-display">
                    <div class="selected-scope">Screen: {self.scope.get_name()}</div>
                    <button class="btn btn-sm btn-outline-secondary change-scope-btn" title="更改作用域"><i class="bi bi-pencil"></i></button>
                </div>
                '''
            elif isinstance(self.scope, UiWindow):
                args_html = f'''
                <div class="target-display">
                    <div class="selected-scope">Window: {self.scope.get_name()}</div>
                    <button class="btn btn-sm btn-outline-secondary change-scope-btn" title="更改作用域"><i class="bi bi-pencil"></i></button>
                </div>
                '''
            elif isinstance(self.scope, UiComponent):
                args_html = f'''
                <div class="target-display">
                    <img src="{self.scope.get_url()}" alt="Scope Element" class="target-image">
                    <div class="selected-scope">Component: {self.scope.get_name()}</div>
                    <button class="btn btn-sm btn-outline-secondary change-scope-btn" title="更改作用域"><i class="bi bi-pencil"></i></button>
                </div>
                '''
        return args_html

    def to_dict(self):
        result = {
            "id": self.id,
            "title": self._generate_title(),
            "args_html": self._generate_args_html(),
            "activities": [activity.to_dict() for activity in self.activities],
        }
        result.update(super().to_dict())

        return result
